﻿Imports SharpDX
Imports SharpDX.Direct2D1

Public Class Npc
    Inherits GameObjBase
    Implements IDisposable

    Public Property BaseImage As Bitmap

    Private BufferedImage As Bitmap

    Public Property Info As NpcXmlInfo

    Private animationId As Int32 = 0

    Private DrawRect As DrawingRectangleF

    Public Property IsAlive As Boolean = True

    Public Sub New(x As Int32, y As Int32, id As Int32, e As Core.Base.GameCoreEventArgs)
        MyBase.New(New Point(x, y), New Size(24, 24))
        Info = NpcXmlInfo.LoadInfo(id, e)
        BaseImage = NpcXmlInfo.LoadImage(id, e)
    End Sub

    'Private Sub SetBounds(width As Int32, height As Int32)
    '    Me.Width = width
    '    Me.Height = height
    '    Me.BaseImage = New Bitmap(BaseImage.
    'End Sub

    Public Overrides Sub Update(e As Core.Base.GameCoreEventArgs)
        UpdateRect(e)
        DoAnimation()
        Fall(e)
        Move()
    End Sub

    Public Sub Move()

    End Sub

    Public Sub Fall(e As Core.Base.GameCoreEventArgs)
        Dim w As World = e.Core.Services.GetService(GetType(World))
        Dim blocksInWIdth As Double = Math.Ceiling(Me.Width / Tile.TileSize)
        Dim blocksInHeight As Double = Math.Floor(Me.Height / Tile.TileSize)
        Dim canFall As Boolean = True
        For intB As Int32 = 0 To blocksInWIdth - 1
            Dim xx As Double = X + intB
            Dim yy As Double = Y + blocksInHeight
            Dim bb As Block
            If (xx > -1 And xx < World.WorldWidth And yy > -1 And yy < World.WorldHeight) Then
                bb = w.level(xx, yy)
            End If
            If (Not IsNothing(bb)) Then
                If (Not bb.Info.Name = "Air") Then
                    canFall = False
                    Exit Sub
                End If
            Else
                canFall = False
                Exit Sub
            End If
        Next

        If (canFall) Then
            Me.Y += 0.75
        End If
    End Sub

    Public Sub DoAnimation()
        animationId += 1
        If (animationId > Info.MaxAnimations) Then
            animationId = 0
        End If
    End Sub

    Public Sub UpdateRect(e As Core.Base.GameCoreEventArgs)
        Dim p As Player = e.Core.Services.GetService(GetType(Player))
        Dim Cam As Camera = p.Cam
        DrawRect = New DrawingRectangleF(CInt((Me.X - Cam.X) * Tile.TileSize), CInt((Me.Y - Cam.Y) * Tile.TileSize), CInt(Me.Width), CInt(Me.Height))
    End Sub

    Public Overrides Sub Draw(e As Core.Base.DrawEventArgs)
        Dim srcRect As New DrawingRectangleF(0, (animationId * Me.Height), Me.Width, Me.Height)
        Dim dstRect As New DrawingRectangleF(0, 0, Me.Width, Me.Height)
        e.Target.DrawBitmap(BaseImage, dstRect, 1.0F, BitmapInterpolationMode.Linear, srcRect)
    End Sub

#Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                If (Not IsNothing(BufferedImage)) Then
                    BufferedImage.Dispose()
                    BufferedImage = Nothing
                End If
                If (Not IsNothing(BaseImage)) Then
                    BaseImage.Dispose()
                    BaseImage = Nothing
                End If
            End If

            ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
            ' TODO: set large fields to null.
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
    'Protected Overrides Sub Finalize()
    '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
